<?php

defined('APP') or die('Vào đúng thu mục đi bạn');

abstract class Core_Model extends MysqlDriver
{
    private $table = null;
    public $sql = null;
    function __construct($table = null)
    {
        $this->table = $table;
        parent::__construct();
        $running = Core::$config["components"]["running"];
        $this->getConnect(Core::$config["components"]["db"][$running]);
    }

    public function find($where = '1=1', $col = '*') {
        $this->sql = 'select ' . $col . ' from '. $this->table .' where ' . $where;
        return parent::find($this->sql);
    }

    public function findBy($where = '1=?', $params = array('1'), $col = '*') {
        $this->sql = 'select ' . $col . ' from '. $this->table .' where '.$where;
        $string = explode('?',$this->sql);
        $this->sql = "";
        for($i=0; $i < count($params); $i++) {
            $this->sql .= $string[$i]."'".$params[$i]."'";
        }
        return parent::find($this->sql);
    }

    public function findOne($where = '1=1', $col = '*') {
        $this->sql = 'select ' . $col . ' from '. $this->table .' where ' . $where;
        parent::find($this->sql);
        if(empty($this->data) || $this->count() == 0) {
            return $this->data;
        } else
            return $this->data = $this->data[0];
    }

    public function findById($id = '1', $col = '*') {
        $this->sql = 'select ' . $col . ' from '. $this->table .' where id = "' . $id . '"';
        parent::find($this->sql);
        if(empty($this->data) || $this->count() == 0) {
            return $this->data;
        } else
            return $this->data = $this->data[0];
    }

    public function delete($where = '1=1') {
        $this->sql = "delete from ". $this->table ." where ". $where;
        $this->data = null;
        return parent::query($this->sql);
    }

    public function update($where = '1=1', $params = array()) {
        if(empty($params) || count($params) == 0) {
            return 0;
        } else {
            $this->sql = "update ". $this->table ." set ". $this->genParams($params, ",") ." where ". $where;
            $this->data = null;
            return parent::query($this->sql);
        }
    }

    public function insert($params = array())
    {
        if(empty($params) || count($params) == 0) {
            return 0;
        } else {
            $this->sql = "insert into ". $this->table ."(". implode(",", array_keys($params)) .") values ('". implode("','", array_values($params)) ."')";
            parent::query($this->sql);
            $this->data = null;
            return parent::getId();
        }
    }

    public function setTable($table)
    {
        $this->table = $table;
    }

    private function genParams($params = array(), $char = ",") {
        $string = array();
        foreach($params as $key => $val){
            $string[] = $key .'="'. $val .'"';
        }
        return implode($char, $string);
    }
}